import request from '@/lib/request';
import type { AxiosRequestConfig } from 'axios';

// 响应数据类型定义
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
  success: boolean;
}

// 文件信息类型
export interface FileInfo {
  id: number;
  name: string;
  url: string;
  size: number;
  type: string;
  uploadTime: string;
}

// 上传统计信息类型
export interface UploadStatistics {
  totalFiles: number;
  totalSize: number;
  todayUploads: number;
  todaySize: number;
}

// 文件上传参数类型
export interface UploadParams {
  file: File;
  userId?: string;
}

// 批量文件上传参数类型
export interface BatchUploadParams {
  files: File[];
  userId?: string;
}

// 文件删除参数类型
export interface DeleteFileParams {
  imageId: number;
  userId?: string;
}

// 文件信息查询参数类型
export interface FileInfoParams {
  imageId: number;
  userId?: string;
}

// 文件存在性检查参数类型
export interface FileExistsParams {
  filePath: string;
}

/**
 * 文件上传 API 服务类
 */
class UploadService {
  /**
   * 单文件上传
   * @param params 上传参数
   * @returns Promise<ApiResponse<FileInfo>>
   */
  static async uploadSingle(params: UploadParams): Promise<ApiResponse<FileInfo>> {
    const formData = new FormData();
    formData.append('file', params.file);

    const config: AxiosRequestConfig = {
      method: 'POST',
      url: '/upload/single',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data',
        ...(params.userId && { User: params.userId }),
      },
    };

    return request<ApiResponse<FileInfo>>(config);
  }

  /**
   * 批量文件上传
   * @param params 批量上传参数
   * @returns Promise<ApiResponse<FileInfo[]>>
   */
  static async uploadBatch(params: BatchUploadParams): Promise<ApiResponse<FileInfo[]>> {
    const formData = new FormData();
    params.files.forEach((file, index) => {
      formData.append('files', file);
    });

    const config: AxiosRequestConfig = {
      method: 'POST',
      url: '/upload/batch',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data',
        ...(params.userId && { User: params.userId }),
      },
    };

    return request<ApiResponse<FileInfo[]>>(config);
  }

  /**
   * 删除文件
   * @param params 删除参数
   * @returns Promise<ApiResponse<boolean>>
   */
  static async deleteFile(params: DeleteFileParams): Promise<ApiResponse<boolean>> {
    const config: AxiosRequestConfig = {
      method: 'POST',
      url: `/upload/delete/${params.imageId}`,
      headers: {
        ...(params.userId && { User: params.userId }),
      },
    };

    return request<ApiResponse<boolean>>(config);
  }

  /**
   * 获取文件信息
   * @param params 文件信息查询参数
   * @returns Promise<ApiResponse<FileInfo>>
   */
  static async getFileInfo(params: FileInfoParams): Promise<ApiResponse<FileInfo>> {
    const config: AxiosRequestConfig = {
      method: 'GET',
      url: `/upload/info/${params.imageId}`,
      headers: {
        ...(params.userId && { User: params.userId }),
      },
    };

    return request<ApiResponse<FileInfo>>(config);
  }

  /**
   * 获取上传统计信息
   * @param userId 用户ID（可选）
   * @returns Promise<ApiResponse<UploadStatistics>>
   */
  static async getUploadStatistics(userId?: string): Promise<ApiResponse<UploadStatistics>> {
    const config: AxiosRequestConfig = {
      method: 'GET',
      url: '/upload/statistics',
      headers: {
        ...(userId && { User: userId }),
      },
    };

    return request<ApiResponse<UploadStatistics>>(config);
  }

  /**
   * 检查文件是否存在
   * @param params 文件存在性检查参数
   * @returns Promise<ApiResponse<boolean>>
   */
  static async checkFileExists(params: FileExistsParams): Promise<ApiResponse<boolean>> {
    const config: AxiosRequestConfig = {
      method: 'GET',
      url: '/upload/exists',
      params: {
        filePath: params.filePath,
      },
    };

    return request<ApiResponse<boolean>>(config);
  }
}



export default UploadService; 